package com.hualan.sfzy.service.course.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hualan.sfzy.entity.CourseCollect;
import com.hualan.sfzy.mapper.course.CourseCollectMapper;
import com.hualan.sfzy.service.course.CourseCollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class CourseCollectServiceImpl implements CourseCollectService {

    @Autowired
    private CourseCollectMapper courseCollectMapper;

    @Override
    public int add(CourseCollect entity) {
        CourseCollect courseCollect = courseCollectMapper.selectItem(entity);
        System.out.println(courseCollect);

        if (courseCollect != null) {
            courseCollect.setDel(0);
           return courseCollectMapper.update(courseCollect);
        }
        return courseCollectMapper.insert(entity);
    }

    @Override
    public int edit(CourseCollect entity) {
        return courseCollectMapper.update(entity);
    }

    @Override
    public int remove(Integer id) {
        return courseCollectMapper.delete(id);
    }

    @Override
    public int removeBatch(Integer[] ids) {
        return courseCollectMapper.deleteBatch(ids);
    }

    @Override
    public List<CourseCollect> find() {
        return courseCollectMapper.selectAll();
    }

    @Override
    public Map<String, Object> findPage(Integer pageNum, Integer pageSize, CourseCollect entity) {
        PageHelper.startPage(pageNum,pageSize);
        List<CourseCollect> courseCollects = courseCollectMapper.select(entity);
        PageInfo<CourseCollect> pageInfo = new PageInfo<>(courseCollects);
        Map<String, Object> result = new HashMap<>();
        result.put("list", pageInfo.getList());
        result.put("total", pageInfo.getTotal());
        result.put("pageNum", pageInfo.getPageNum());
        result.put("pageSize", pageInfo.getPageSize());
        result.put("pages", pageInfo.getPages());
        return result;
    }

    @Override
    public CourseCollect findById(Integer id) {
        return courseCollectMapper.selectById(id);
    }
}
